#include"touch.h"
#include"lcd.h"
#include"bmp.h"
#include"video.h"
#include"music.h"
#include"dir.h"
 int status = 0;
// 开始播放
void start_mplayer(char *str)
{
	char cmd[256] = {0};
	
	sprintf(cmd, "mplayer -slave -quiet -geometry 0:0 -zoom -x 800 -y 380 %s &", str);	// 拼接mplayer命令参数
	printf("cmd: %s\n", cmd);
	
	system(cmd);	// & 后台进程
}

// 暂停播放
void stop_mplayer(void)
{
	system("killall -SIGSTOP mplayer");	//停止madplay进程

}

// 继续播放
void continue_mplayer(void)
{
	system("killall -SIGCONT mplayer");	//继续madplay进程
}


// 杀死播放
void kill_mplayer(void)
{
	system("killall -SIGKILL mplayer");	//杀死madplay进程
}



void video(LCD *lcd,TouchScreen *ts)
{
    char (*video_file)[256] = calloc(100,256);
    if (video_file == NULL)
    {
        perror("video_file calloc failed");
        exit(1);
    }
    
    int count = 0;
    count = retrieve_file("./video",video_file,".avi",100);
	Coordinate coor;
    anybmp(0,380,"res/mplayer.bmp",lcd);
	int index = 0;
    start_mplayer(video_file[index]);
	while(1)
	{
		// 获取坐标
		ts_press(&coor, ts);
		printf("(%d, %d)\n", coor.x, coor.y);
		
		if(coor.y>0 && coor.y<100 && coor.x>750 && coor.x<800) //退出
        {
            kill_mplayer();
            break;
        }
			
		else if(coor.x>0 && coor.x<250 && coor.y>380 && coor.y<490)
        {
            printf("上一个视频\n");
            kill_mplayer();
            index = (--index == -1) ? count-1 : index;
            start_mplayer(video_file[index]);
        }
			
		else if(coor.x>600 && coor.x<800 && coor.y>380 && coor.y<480)
        {
            printf("下一个视频\n");
            kill_mplayer();
            index = (++index == count) ? 0 : index;
            start_mplayer(video_file[index]);
        }
			
		else if (coor.y<480 && coor.y>380 && coor.x>360 && coor.x<440 && status == 0)
        {
                printf("停止播放\n");
                stop_mplayer();
                status = 1;
        }
        else if (coor.y<480 && coor.y>380 && coor.x>360 && coor.x<440 && status == 1)
        {
                printf("开始播放\n");
                continue_mplayer();
                status = 0;
        }
	}
} 